<template>
  <div id="app">
    <keep-alive>
      <router-view v-if="$route.meta.keepAlive"></router-view>
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive"></router-view>
  </div>
</template>

<script>
import AppEventListener from '@/mixin/app-event-listener'
import { isHaveNetwork } from '@/utils'

export default {
  name: 'App',
  mixins: [AppEventListener],
  mounted() {
    const { android } = window

    if (android) {
      android.loadComplete()
    }
    this.handleAddListener()

    // 检查网络
    if (!isHaveNetwork()) {
      this.$store.dispatch('showModel')
    }
  },
  methods:{
    handleAddListener(){
      window.addEventListener('sw.updated', event => {
        const e = event;
        const reloadSW = async () => {
          const worker = e.detail && e.detail.waiting;
          if (!worker) {
            return true;
          }
          await new Promise((resolve, reject) => {
            const channel = new MessageChannel();
            worker.postMessage(
              {
                type: 'skip-waiting',
              },
              [channel.port1],
            );
          });

          window.location.reload(true);
          return true;
        };
        reloadSW();
      });
    }
  }
}
</script>

<style></style>
